package com.ayf.payment.game.api.service.payment;

import com.ayf.payment.game.api.cache.redis.RedisCacheUtil;
import com.ayf.payment.game.api.config.SystemInitialization;
import com.ayf.payment.game.api.dto.OrderAndMerchantDTO;
import com.ayf.payment.game.api.entity.Order;
import com.ayf.payment.game.api.entity.Partition;
import com.ayf.payment.game.api.mapper.OrderMapper;
import com.ayf.payment.game.api.service.*;
import com.ayf.payment.game.api.wx.WxSendMsgUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;

/**
 *
 *
 * @author wwl
 * @date 2020/3/19 15:26
 */
@Component
public abstract class IPaymentService {

    @Resource
    public OrderMapper orderMapper;

    @Autowired
    public RedisCacheUtil redisCacheUtil;

    @Autowired
    MerchantConfigService merchantConfigService;

    @Autowired
    protected MerchantService merchantService;

    @Autowired
    public OrderService orderService;

    @Autowired
    public ChannelTradeAmountRandomRuleService channelTradeAmountRandomRuleService;

    @Autowired
    public SystemInitialization systemInitialization;
    @Autowired
    public WxSendMsgUtils wxSendMsgUtils;

    @Autowired
    public SignPayProductService signPayProductService;

    @Resource
    public MerchantRepository merchantRepository;

    @Autowired
    private MerchantTradeAmountRandomRuleService merchantTradeAmountRandomRuleService;

    public BigDecimal getActualOrderAmt(BigDecimal orderAmt,Integer channelId,Integer merchantId){
        BigDecimal actualOrderAmt = orderAmt;
        boolean existMerchantTradeRule = merchantTradeAmountRandomRuleService.existMerchantTradeRule(merchantId);
        if (existMerchantTradeRule){
            BigDecimal merchantTradeAmountRandomAmt = merchantTradeAmountRandomRuleService.getMerchantTradeAmountRandomRule(merchantId,orderAmt);
            actualOrderAmt = actualOrderAmt.add(merchantTradeAmountRandomAmt);
        } else {
            BigDecimal channelTradeAmountRandomAmt = channelTradeAmountRandomRuleService.getChannelTradeAmountRandomRule(channelId,orderAmt);
            actualOrderAmt = actualOrderAmt.add(channelTradeAmountRandomAmt);
        }
        return actualOrderAmt;
    }

    public abstract Map<String, Object> paymentUtil(Order order, Partition partition, String orderCreateStr, String traceId, String redirectUrl);

    public abstract void queryPayOrder(OrderAndMerchantDTO dto, String traceId);
}
